<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");
require_once("./_inc/frameFunction.php");

$errMsg = "";
$process_unit = sxREQ('pu');
$frame = sxREQ('frame');

if(sxREQ("reqtype") == "save"){
	$errMsg .= frameMod();
}
if(sxREQ("reqtype") == "layoutChange"){
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,9)!='pu_alias_') continue;
		unset($_REQUEST[$nX]);
	}
}

$sSQL = "SELECT l.* FROM layout l " .
			" WHERE l.module='".substr($process_unit,0,4)."' " .
			" ORDER BY l.layout";
$objSDAO->query($sSQL, "frm");
$aValL = array(''=>'-- select a layout');
while($row = $objSDAO->fetch("frm")){
	$aValL[$row["layout"]] = $row["layout"].' - '.$row["description"];
}
$sSQL = "SELECT f.*, fa.alias, la.layout_alias, la.entity " .
		"    ,pa.entity AS pa_entity, pa.description as pa_des " .
		" FROM frame f " .
		" LEFT JOIN layout_alias la ON la.layout = f.layout " .
		" LEFT JOIN frame_alias_match fa ON fa.process_unit = f.process_unit AND fa.frame = f.frame " .
		"    AND fa.layout_alias = la.layout_alias " .
		" LEFT JOIN pu_alias pa ON pa.process_unit = f.process_unit AND pa.alias = fa.alias " .
		" WHERE f.process_unit='$process_unit' AND f.frame='$frame' " .
		" ORDER BY la.layout_alias";
$objSDAO->query($sSQL, "frm");
$row = $objSDAO->fetch("frm");
sxRDef('new_frame',$row['frame']);
sxRDef('description',$row['description']);
sxRDef('layout',$row['layout']);
sxRDef('display_only',$row['display_only']);
sxRDef('Note',$row['Note']);
while($row) {
	sxRDef('pu_alias_'.$row['layout_alias'], $row['alias']);
	sxRDef('la_entity_'.$row['layout_alias'], $row['entity']);
	sxRDef('pa_entity_'.$row['layout_alias'], $row['pa_entity']);
	sxRDef('pa_des_'.$row['layout_alias'], $row['pa_des']);
	$row = $objSDAO->fetch("frm");
}
$sSQL = "SELECT pa.* FROM pu_alias pa " .
			" WHERE pa.process_unit='".$process_unit."' " .
			" ORDER BY pa.alias";
$objSDAO->query($sSQL, "lal");
$aValPA = array();
while($aX = $objSDAO->fetch("lal")){
	$aValPA[$aX["alias"]] = $aX["alias"].' - '.$aX["entity"];
}
//dbg_("aValPA=".print_r($aValPA,1));
//output HTML
$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));

echo getPuHeader($process_unit);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0)
										)
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('pu');
echo getHtmlHidden('frame');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Frame</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Frame*</td>" .
			"<td class=\"text01\">".getHtmlText('new_frame',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Layout*</td>" .
			"<td class=\"text01\">");
$aOptL = array('onChange'=>"objFrmSubm('layoutChange');");
echo getHtmlSelect('layout', $aValL,'@REQ',$aOptL);
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
$aValYN = array('Y'=>'Yes','N'=>'No');
echo("<tr>" .
			"<td class=\"labl01\">Display only</td>" .
			"<td class=\"text01\">" .
			getHtmlRadio('display_only',$aValYN).
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',80,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Aliases</td>" .
		"</tr>\n");
	echo("<tr>" .
				"<td class=\"tabh01\" width='30%'>Layout alias</td>" .
				"<td class=\"tabh01\" width='70%'>Process-unit alias</td>" .
			"</tr>\n");
foreach($_REQUEST as $nX => $alias) {
	if (substr($nX,0,9)!='pu_alias_') continue;
	$layoutAlias = substr($nX,9);
	$laEntity = sxREQ('la_entity_'.$layoutAlias);
	$paEntity = sxREQ('pa_entity_'.$layoutAlias);
	$paDes = sxREQ('pa_des_'.$layoutAlias);
	echo "<tr>";
	echo "<td class=\"text01\">".$layoutAlias.((empty($laEntity))?'':" - ".$laEntity)."</td>";
	echo "<td class=\"text01\">";
	if (preg_match("/[WX]/",$alias)) {
		echo $alias." - " .$paDes;
	} else {
		$aValX = array();
		foreach($aValPA as $kX => $dX) {
			if ($laEntity == substr($dX,4)) {
				$aValX[$kX]=$dX;
			}
		}
		if (count($aValX)==1) {
			echo getHtmlHidden('pu_alias_'.$layoutAlias, key($aValX));
			echo current($aValX);
		} elseif (count($aValX)>1) {
			echo getHtmlSelect('pu_alias_'.$layoutAlias, $aValX);
		} else {
			echo getHtmlHidden('pu_alias_'.$layoutAlias, '');
			echo "-- unmatchable --";
		}
	}
	echo getHtmlHidden('la_entity_'.$layoutAlias, $laEntity);
	echo getHtmlHidden('pa_entity_'.$layoutAlias, $paEntity);
	echo getHtmlHidden('pa_des_'.$layoutAlias, $paDes);
	echo ("</td>" .
			"</tr>\n");
	$row = $objSDAO->fetch("frm");		
}
echo("</table>");
echo $sCmd;
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function frameMod(){
 	global $objSDAO;
	$errMsg = "";
	if(sxREQ("new_frame") == "")
		$errMsg .= "missing frame<br>";
	if(preg_match("/[^0-9a-zA-Z_]",sxREQ("new_frame")))
		$errMsg .= "frame: only letter, numbers and _<br>";
	if(sxREQ("layout") == "")
		$errMsg .= "unselected layout<br>";
	if (sxREQ("new_frame") != sxREQ("frame")) {
		$sSQL = "SELECT * FROM frame " .
				"WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('new_frame')."";	
		$objSDAO->query($sSQL);
		if ($aX=$objSDAO->fetch()) {
			$errMsg .= "existing frame<br>";
		}
	}
	$aMatch = array();
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,9)!='pu_alias_') continue;
		if ($vX=='') {
			$errMsg .= "alias ".substr($nX,9)." unmatched<br>";
		}
		$aMatch[substr($nX,9)] = $vX;
	}
	if($errMsg != ""){return $errMsg;}
	
	$sSQL = "UPDATE frame SET frame = '".sxRAS('new_frame')."'" .
			", layout='".sxRAS('layout')."' " .
			", description='".sxRAS('description')."' " .
			", display_only='".sxRAS('display_only')."' " .
			", note='".sxRAS('note')."' " .
			" WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('frame')."' ";	
	$objSDAO->exec($sSQL);
	if (sxREQ("new_frame") != sxREQ("frame")) {
		$sSQL = "UPDATE frame_alias_match SET frame = '".sxRAS('new_frame')."' " .
				"WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('frame')."' ";	
		$objSDAO->exec($sSQL);
		if ($aX=$objSDAO->fetch()) {
			$errMsg .= "existing frame<br>";
		}
	}
	
	$sSQL = "SELECT * FROM frame_alias_match "
		." WHERE process_unit = '".sxRAS('pu')."' AND frame = '".sxRAS('new_frame')."' ";
	$objSDAO->query($sSQL,'fam');
	while($aX = $objSDAO->fetch('fam')) {
		if (isset($aMatch[$aX['layout_alias']])) {
			$sSQL = "UPDATE frame_alias_match SET alias = '".$aMatch[$aX['layout_alias']]."' " .
				" WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('new_frame')."' "
				." AND layout_alias = '".$aX['layout_alias']."'";	
			$objSDAO->exec($sSQL);
			unset($aMatch[$aX['layout_alias']]);
			continue;
		}
		$sSQL = "DELETE FROM frame_alias_match " .
				" WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('new_frame')."' "
				." AND layout_alias = '".$aX['layout_alias']."'";	
		$objSDAO->exec($sSQL);
	}
	foreach($aMatch as $layout_alias => $alias) {
		$sSQL = "INSERT INTO frame_alias_match (process_unit,frame,alias,layout,layout_alias)" 
						."VALUES('".sxRAS('pu')."', '".sxRAS('new_frame')."', '$alias' "
						.", '".sxRAS('layout')."', '$layout_alias')";
		$objSDAO->exec($sSQL);
	}
	
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}
